package com.settlement.system.controller;

import com.settlement.system.common.enums.CostAllocationEnum;
import com.settlement.system.model.entity.common.BasePager;
import com.settlement.system.model.entity.common.CommonPlugIn;
import com.settlement.system.model.entity.common.SinglePage;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IWpPlugInService;
import com.settlement.system.common.util.common.FasUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 */
@Slf4j
@RestController
@RequestMapping("/wp/plugin/")
public class WpPlugInController {

    @Resource
    private IWpPlugInService wpPlugInService;

    @ApiOperation("唯品账单")
    @RequestMapping("billNumber")
    public ObjectDataResponse getBillNumber(@RequestBody BasePager<Map<String,Object>> model){
        Map<String,Object> result = new HashMap<>(16);
        try {
            FasUtil.formatInQueryCondition(model.getModel(),"shopNo","settleAccountPeriod","billNumberType");
            int count = wpPlugInService.getBillNumberCount(model.getModel());
            List<CommonPlugIn> rows = wpPlugInService.getBillNumber(new SinglePage(model),model.getModel(),
                    model.getOrderByField(),model.getOrderBy());
            result.put("rows",rows);
            result.put("total",count);
        } catch (Exception ex){
            ex.printStackTrace();
            return new ObjectDataResponse(500,"获取列表失败，请联系管理员！");
        }
        return new ObjectDataResponse<Map>(result,200,"获取列表成功");
    }

    @ApiOperation("唯品结算期间")
    @RequestMapping("settlePeroid")
    public ObjectDataResponse getSettlePeroid(@RequestBody BasePager<Map<String,Object>> model){
        Map<String,Object> result = new HashMap<>(16);
        try {
            int count = wpPlugInService.getSettlePeriodCount(model.getModel());
            List<CommonPlugIn> rows = wpPlugInService.getSettlePeriod(new SinglePage(model),model.getModel(),
                    model.getOrderByField(),model.getOrderBy());
            result.put("rows",rows);
            result.put("total",count);
        } catch (Exception ex){
            ex.printStackTrace();
            return new ObjectDataResponse(500,"获取列表失败，请联系管理员！");
        }
        return new ObjectDataResponse<Map>(result,200,"获取列表成功");
    }

    @ApiOperation("唯品档期")
    @RequestMapping("calendar")
    public ObjectDataResponse getCalendar(@RequestBody BasePager<Map<String,Object>> model){
        Map<String,Object> result = new HashMap<>(16);
        try {
            int count = wpPlugInService.getCalendarCount(model.getModel());
            List<CommonPlugIn> rows = wpPlugInService.getCalendar(new SinglePage(model),model.getModel(),
                    model.getOrderByField(),model.getOrderBy());
            result.put("rows",rows);
            result.put("total",count);
        } catch (Exception ex){
            ex.printStackTrace();
            return new ObjectDataResponse(500,"获取列表失败，请联系管理员！");
        }
        return new ObjectDataResponse<Map>(result,200,"获取列表成功");
    }

    @ApiOperation("唯品档期")
    @RequestMapping("getOrderSchedule")
    public ObjectDataResponse getOrderSchedule(@RequestBody BasePager<Map<String,Object>> model){
        Map<String,Object> result = new HashMap<>(16);
        try {
            int count = wpPlugInService.getOrderScheduleCount(model.getModel());
            List<CommonPlugIn> rows = wpPlugInService.getOrderSchedule(new SinglePage(model),model.getModel(),
                    model.getOrderByField(),model.getOrderBy());
            result.put("rows",rows);
            result.put("total",count);
        } catch (Exception ex){
            ex.printStackTrace();
            return new ObjectDataResponse(500,"获取列表失败，请联系管理员！");
        }
        return new ObjectDataResponse<Map>(result,200,"获取列表成功");
    }

    /**
     * 带全选的
     * @param model
     * @return
     */
    @ApiOperation("唯品档期")
    @RequestMapping("getAllOrderSchedule")
    public ObjectDataResponse getAllOrderSchedule(@RequestBody BasePager<Map<String,Object>> model){
        Map<String,Object> result = new HashMap<>(16);
        try {
            int count = wpPlugInService.getOrderScheduleCount(model.getModel());
            List<CommonPlugIn> rows = wpPlugInService.getOrderSchedule(new SinglePage(model),model.getModel(),
                    model.getOrderByField(),model.getOrderBy());
            model.setPageNum(1);
            model.setPageSize(1000);
            List<CommonPlugIn> allRows = wpPlugInService.getOrderSchedule(new SinglePage(model),model.getModel(),
                    model.getOrderByField(),model.getOrderBy());
            result.put("rows",rows);
            result.put("allRows",allRows);
            result.put("total",count);
        } catch (Exception ex){
            ex.printStackTrace();
            return new ObjectDataResponse(500,"获取列表失败，请联系管理员！");
        }
        return new ObjectDataResponse<Map>(result,200,"获取列表成功");
    }


    @ApiOperation("唯品公司")
    @RequestMapping("company")
    public ObjectDataResponse getCompany(HttpServletRequest request){
        try {
            Map<String,Object> param = buildParam(request);
            Map<String,Object> result = new HashMap<>(16);
            List<CommonPlugIn> list = wpPlugInService.getCompany(param);
            result.put("rows",list);
            result.put("total",list.size());
            return  new ObjectDataResponse<Map>(result,200,"获取列表成功");
        } catch (Exception ex){
            ex.printStackTrace();
            return  new ObjectDataResponse<Map>(500,"获取列表失败，请联系管理员！");
        }
    }

    @ApiOperation("账单编码-不包括禁用的")
    @RequestMapping("billTypeList")
    public ObjectDataResponse getBillTypeList(HttpServletRequest request){
        try {
            Map<String,Object> param = buildParam(request);
            Map<String,Object> result = new HashMap<>(16);
            List<CommonPlugIn> list = wpPlugInService.getBillTypeList(param);
            result.put("rows",list);
            result.put("total",list.size());
            return  new ObjectDataResponse<Map>(result,200,"获取列表成功");
        } catch (Exception ex){
            ex.printStackTrace();
            return  new ObjectDataResponse<Map>(500,"获取列表失败，请联系管理员！");
        }
    }

    @ApiOperation("账单大类")
    @RequestMapping("billCategoryList")
    public ObjectDataResponse billCategoryList(HttpServletRequest request){
        try {
            Map<String,Object> param = buildParam(request);
            Map<String,Object> result = new HashMap<>(16);
            List<CommonPlugIn> list = wpPlugInService.getBillCategoryList(param);
            for (CommonPlugIn commonPlugIn : list) {
                String code = commonPlugIn.getCode();
                String nameByNo = CostAllocationEnum.BillCategoryEnum.getNameByNo(code);
                commonPlugIn.setName(nameByNo);
            }
            result.put("rows",list);
            result.put("total",list.size());
            return  new ObjectDataResponse<Map>(result,200,"获取列表成功");
        } catch (Exception ex){
            ex.printStackTrace();
            return  new ObjectDataResponse<Map>(500,"获取列表失败，请联系管理员！");
        }
    }

    @ApiOperation("账单大类")
    @RequestMapping("billCategoryInUseList")
    public ObjectDataResponse billCategoryInUseList(HttpServletRequest request){
        try {
            Map<String,Object> param = buildParam(request);
            Map<String,Object> result = new HashMap<>(16);
            List<CommonPlugIn> list = wpPlugInService.getBillCategoryInUseList(param);
            for (CommonPlugIn commonPlugIn : list) {
                String code = commonPlugIn.getCode();
                String nameByNo = CostAllocationEnum.BillCategoryEnum.getNameByNo(code);
                commonPlugIn.setName(nameByNo);
            }
            result.put("rows",list);
            result.put("total",list.size());
            return  new ObjectDataResponse<Map>(result,200,"获取列表成功");
        } catch (Exception ex){
            ex.printStackTrace();
            return  new ObjectDataResponse<Map>(500,"获取列表失败，请联系管理员！");
        }
    }

    @ApiOperation("账单编码")
    @RequestMapping("billTypeListAll")
    public ObjectDataResponse getBillTypeListAll(HttpServletRequest request){
        try {
            Map<String,Object> param = buildParam(request);
            Map<String,Object> result = new HashMap<>(16);
            List<CommonPlugIn> list = wpPlugInService.getBillTypeListAll(param);
            result.put("rows",list);
            result.put("total",list.size());
            return  new ObjectDataResponse<Map>(result,200,"获取列表成功");
        } catch (Exception ex){
            ex.printStackTrace();
            return  new ObjectDataResponse<Map>(500,"获取列表失败，请联系管理员！");
        }
    }

    @ApiOperation("渠道")
    @RequestMapping("searchChannelList")
    public ObjectDataResponse searchChannelList(HttpServletRequest request){
        try {
            Map<String,Object> param = buildParam(request);
            Map<String,Object> result = new HashMap<>(16);
            List<CommonPlugIn> list = wpPlugInService.searchChannelList(param);
            result.put("rows",list);
            result.put("total",list.size());
            return  new ObjectDataResponse<Map>(result,200,"获取列表成功");
        } catch (Exception ex){
            return  new ObjectDataResponse<Map>(500,"获取列表失败，请联系管理员！");
        }
    }

    @ApiOperation("大类")
    @RequestMapping("searchCategoryList")
    public ObjectDataResponse searchCategoryList(HttpServletRequest request){
        try {
            Map<String,Object> param = buildParam(request);
            Map<String,Object> result = new HashMap<>(16);
            List<CommonPlugIn> list = wpPlugInService.searchCategoryList(param);
            result.put("rows",list);
            result.put("total",list.size());
            return  new ObjectDataResponse<Map>(result,200,"获取列表成功");
        } catch (Exception ex){
            ex.printStackTrace();
            return  new ObjectDataResponse<Map>(500,"获取列表失败，请联系管理员！");
        }
    }

    @ApiOperation("联表查询的表的列表")
    @RequestMapping("searchQueryTableList")
    public ObjectDataResponse searchQueryTableList(HttpServletRequest request){
        try {
            Map<String,Object> param = buildParam(request);
            Map<String,Object> result = new HashMap<>(16);
            List<CommonPlugIn> list = wpPlugInService.searchQueryTableList(param);
            result.put("rows",list);
            result.put("total",list.size());
            return  new ObjectDataResponse<Map>(result,200,"获取列表成功");
        } catch (Exception ex){
            log.error("searchQueryTableList error",ex);
            return  new ObjectDataResponse<Map>(500,"获取列表失败，请联系管理员！");
        }
    }



    @ApiOperation("凭证类型")
    @RequestMapping("voucherTypeList")
    public ObjectDataResponse getVoucherTypeList(HttpServletRequest request){
        try {
            Map<String,Object> param = buildParam(request);
            Map<String,Object> result = new HashMap<>(16);
            List<CommonPlugIn> list = wpPlugInService.getVoucherTypeList(param);
            result.put("rows",list);
            result.put("total",list.size());
            return  new ObjectDataResponse<Map>(result,200,"获取列表成功");
        } catch (Exception ex){
            return  new ObjectDataResponse<Map>(500,"获取列表失败，请联系管理员！");
        }
    }


    @ApiOperation("唯品店铺")
    @RequestMapping(value = "shop",method = RequestMethod.GET)
    public ObjectDataResponse getShopByGet(HttpServletRequest request){
        try {
            Map<String,Object> param = buildParam(request);
            Map<String,Object> result = new HashMap<>(16);
            List<CommonPlugIn> list = wpPlugInService.getShop(param);
            result.put("rows",list);
            result.put("total",list.size());
            return  new ObjectDataResponse<Map>(result,200,"获取列表成功");
        } catch (Exception ex){
            return  new ObjectDataResponse<Map>(500,"获取列表失败，请联系管理员！");
        }
    }

    @ApiOperation("唯品店铺")
    @RequestMapping(value = "shop",method = RequestMethod.POST)
    public ObjectDataResponse getShopByPost(@RequestBody BasePager<Map<String,Object>> model){
        try {
            Map<String,Object> result = new HashMap<>(16);
            List<CommonPlugIn> list = wpPlugInService.getShop(model.getModel());
            result.put("rows",list);
            result.put("total",list.size());
            return  new ObjectDataResponse<Map>(result,200,"获取列表成功");
        } catch (Exception ex){
            return  new ObjectDataResponse<Map>(500,"获取列表失败，请联系管理员！");
        }
    }

    /**
     * 构造请求参数
     * @param request
     * @return
     */
    protected Map<String,Object> buildParam(HttpServletRequest request){
        Map<String,Object> params = new HashMap<>(16);
        for(Map.Entry<String,String[]> entry :request.getParameterMap().entrySet()){
            String value = entry.getValue()[0];
            if(!StringUtils.isEmpty(value)){
                params.put(entry.getKey(),value);
            }
        }
        FasUtil.formatInQueryCondition(params,"shopNo","billNumberType","settleAccountPeriod");
        return params;
    }

}
